package com.lhwl.it.yh.action;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.lhwl.it.util.PageBean;
import com.lhwl.it.util.Pmap;
import com.lhwl.it.util.RequestMap;
import com.lhwl.it.yh.service.YhService;

@Controller
@RequestMapping("/itmanager/yh")
public class YhAction {
	
	private Log log = LogFactory.getLog(this.getClass());
	@Resource
	private YhService yhService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String index(HttpServletRequest request,HttpServletResponse response) throws Exception{
		
		return "/WEB-INF/jsp/yh/yhgl";
	}
	
	
	@RequestMapping(value="/mm",method=RequestMethod.POST)
	public String updateMM(HttpServletRequest request,HttpServletResponse response) throws Exception{
		HashMap map=new RequestMap(request);
		String jmm=(String) map.get("jmm");
		HashMap yh=(HashMap) request.getSession().getAttribute("yh");
		String id=yh.get("id").toString();
		map.put("id", id);
		HashMap myh=this.yhService.getMap("YhMapper.getYh", map);
		String mm=(String) myh.get("mm");
		if(!jmm.equals(mm)){
			response.getWriter().print("wrong");
		}else{
			try{
				this.yhService.update("YhMapper.updateMM", map);
				response.getWriter().print("success");
			}catch(Exception e){
				e.printStackTrace();
				response.getWriter().print("error");
			}
		}
		return null;
	}
	
	@RequestMapping(value="/jswh")
	public String jswh(HttpServletRequest request,HttpServletResponse response)throws Exception{
		return "/WEB-INF/jsp/yh/jswh";
	}
	
	@RequestMapping(value="/gnwh")
	public String gnwh(HttpServletRequest request,HttpServletResponse response)throws Exception{
		return "/WEB-INF/jsp/yh/gnwh";
	}
	
	@RequestMapping(value="/yhwh")
	public String yhwh(HttpServletRequest request,HttpServletResponse response)throws Exception{
		List rsList = this.yhService.list("YhMapper.getJs", null);
		request.setAttribute("rslist", rsList);
		return "/WEB-INF/jsp/yh/yhwh";
	}
	
	@RequestMapping(value="/gnwhcz")
	public String gnwhcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap<String,String> map=new RequestMap(request);
		String tag = map.get("tag");
		if("add".equals(tag)){
			this.yhService.insert("YhMapper.insertGn", map);
		}else if("edit".equals(tag)){
			this.yhService.insert("YhMapper.updateGn", map);
		}else if("find".equals(tag)){
			HashMap m = this.yhService.getMap("YhMapper.getGn", map);
			JSONObject jsonO = JSONObject.fromObject(m);
			response.getWriter().print(jsonO);
			return null;
		}else{
			this.yhService.insert("YhMapper.deleteGn", map);
		}
		response.getWriter().print("success");
		return null;
	}
	
	@RequestMapping(value="/yhwhcz")
	public String yhwhcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap<String,String> map=new RequestMap(request);
		String tag = map.get("tag");
		if("add".equals(tag)){
			this.yhService.insert("YhMapper.insert", map);
		}else if("edit".equals(tag)){
			this.yhService.insert("YhMapper.update", map);
		}else if("find".equals(tag)){
			HashMap m = this.yhService.getMap("YhMapper.getYh", map);
			JSONObject jsonO = JSONObject.fromObject(m);
			response.getWriter().print(jsonO);
			return null;
		}else{
			String ids = request.getParameter("ids");
			if(ids != null){
				String[] tmp = ids.split(",");
				HashMap m = new HashMap();
				for(int i=0;i<tmp.length;i++){
					m.put("id",tmp[i]);
					this.yhService.insert("YhMapper.delete", m);
				}
			}
		}
		response.getWriter().print("success");
		return null;
	}
	
	@RequestMapping(value="/czmm")
	public String czmm(HttpServletRequest request,HttpServletResponse response)throws Exception{
		try{
			String ids = request.getParameter("ids");
			if(ids != null){
				String[] tmp = ids.split(",");
				HashMap m = new HashMap();
				m.put("mm", "123456");
				for(int i=0;i<tmp.length;i++){
					m.put("id",tmp[i]);
					this.yhService.update("YhMapper.updateMM", m);
				}
			}
			response.getWriter().print("success");
		}catch(Exception e){
			response.getWriter().print("error");
		}
		return null;
	}
	
	
	@RequestMapping(value="/gnwhjson")
	public String gnwhjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		PageBean pageBean = this.yhService.getPage("YhMapper.getGn", map);
		JSONObject jsonO = new JSONObject();
		jsonO.put("rows", pageBean.getList());
		jsonO.put("total", pageBean.getRowscount());
		response.getWriter().print(jsonO);
		return null;
	}
	
	@RequestMapping(value="/yhwhjson")
	public String yhwhjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		PageBean pageBean = this.yhService.getPage("YhMapper.getYh", map);
		JSONObject jsonO = new JSONObject();
		jsonO.put("rows", pageBean.getList());
		jsonO.put("total", pageBean.getRowscount());
		response.getWriter().print(jsonO);
		return null;
	}
	
	@RequestMapping(value="/jsqx")
	public String jsqx(HttpServletRequest request,HttpServletResponse response)throws Exception{
		return "/WEB-INF/jsp/yh/jsqx";
	}
	
	@RequestMapping(value="/jswhjson")
	public String jswhjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		PageBean pageBean = this.yhService.getPage("YhMapper.getJs", map);
		JSONObject jsonO = new JSONObject();
		jsonO.put("rows", pageBean.getList());
		jsonO.put("total", pageBean.getRowscount());
		response.getWriter().print(jsonO);
		return null;
	}
	
	@RequestMapping(value="/jswhcz")
	public String jswhcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map=new RequestMap(request);
		String tag = (String)map.get("tag");
		try{
			if("add".equals(tag)){
				this.yhService.insert("YhMapper.insertJs", map);
			}else if("edit".equals(tag)){
				this.yhService.insert("YhMapper.updateJs", map);
			}
			else if("delete".equals(tag)){
				this.yhService.insert("YhMapper.deleteJs", map);
			}
			response.getWriter().print("success");
		}catch(Exception e){
			//System.out.println(e);
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value="/qxwh")
	public String qxwh(HttpServletRequest request,HttpServletResponse response)throws Exception{
		return "/WEB-INF/jsp/yh/qxwh";
	}
	
	@RequestMapping(value="/qxgnwh")
	public String qxgnwh(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		List rsList = this.yhService.list("yhMapper.getQxgn", map);
		request.setAttribute("rslist", rsList);
		request.setAttribute("map", map);
		return "/WEB-INF/jsp/yh/gnsz";
	}
	
	@RequestMapping(value="/qxgnwhbc")
	public String qxgnwhbc(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		this.yhService.delete("yhMapper.deleteQxgn", map);
		String[] vals = request.getParameterValues("gnsz");
		if(vals == null || vals.length==0 ){
			map.put("ck", "open");
		}else{
			for(int i=0;i<vals.length;i++){
				HashMap m = new HashMap();
				m.put("qxid", map.get("qxid"));
				m.put("gnid", vals[i]);
				this.yhService.insert("yhMapper.insertQxgn", m);
			}
			map.put("ck", "closed");
		}
		this.yhService.update("yhMapper.updateQx1", map);
		response.getWriter().print("操作成功！");
		request.setAttribute("map", map);
		return null;
	}
	
	@RequestMapping(value="/qxwhjson")
	public String qxwhjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		HashMap m = new HashMap();
		if(map.get("id")!=null){
			m.put("id", map.get("id"));
		}
		List<HashMap> rsList = this.yhService.list("YhMapper.getMenu", m);
		JSONArray jsonO = JSONArray.fromObject(rsList);
		response.getWriter().print(jsonO);
		return null;
	}
	
	@RequestMapping(value="/qxjson")
	public String getQx(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		HashMap m = new HashMap();
		if(map.get("id")!=null){
			String[] tmp = map.get("id").toString().split(",");
			m.put("id", tmp[tmp.length-1]);
		}
		List<HashMap> rsList = null;
		if(map.get("jsid")==null){
			rsList = this.yhService.list("YhMapper.getQx", m);
		}else{
			m.put("jsid", map.get("jsid"));
			rsList = this.yhService.list("YhMapper.getJsqx", m);
			if(rsList == null || rsList.size() == 0){
				rsList = this.yhService.list("yhMapper.getJsqxgn", m);
			}
		}
		for(HashMap hm : rsList){
			String id = hm.get("id").toString();
			if(id.indexOf(":")!=-1){
				((Pmap)hm).putProtoType("iconCls", "icon-edit");
			}
			if(map.get("id")!=null){
				hm.put("id", map.get("id") + "," + id);
			}
		}
		//System.out.println(rsList);
		JSONArray jsonO = JSONArray.fromObject(rsList);
		response.getWriter().print(jsonO);
		return null;
	}
	
	@RequestMapping(value="/jsqxcz")
	public String jsqxcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap<String,String> map = new RequestMap(request);
		String qxid = map.get("qxid");
		String[] qxids = qxid.split(",");
		this.yhService.delete("YhMapper.deleteJsqx", map);
		this.yhService.delete("YhMapper.deleteJsqxgn", map);
		for(int i=0;i<qxids.length;i++){
			HashMap m = new HashMap();
			m.put("jsid", map.get("jsid"));
			String[] tmp = qxids[i].split(":");
			if(tmp.length>1){
				m.put("qxid", tmp[0]);
				m.put("gnid", tmp[1]);
				this.yhService.insert("YhMapper.insertJsqxgn", m);
			}else{
				m.put("qxid", qxids[i]);
				this.yhService.insert("YhMapper.insertJsqx", m);
			}
		}
		response.getWriter().print("success");
		return null;
	}
	
	
	@RequestMapping(value="/qxwhcz")
	public String qxwhcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		RequestMap map=new RequestMap(request);
		String tag = (String)map.get("tag");
		if(!"null".equals(map.get("_parentid")) && null != map.get("_parentid")){
			map.put("fjd", map.get("_parentid"));
		}else{
			map.put("fjd", "0");
		}
		try{
			if("add".equals(tag)){
				this.yhService.insert("YhMapper.insertQx", map);
				if(!"0".equals(map.get("fjd").toString())){
					HashMap m = new HashMap();
					m.put("id", map.get("fjd"));
					m.put("state", "closed");
					this.yhService.update("yhMapper.updateQxState", m);
				}
			}else if("edit".equals(tag)){
				this.yhService.insert("YhMapper.updateQx", map);
			}
			else if("delete".equals(tag)){
				if(!"0".equals(map.get("fjd").toString())){
					HashMap m = new HashMap();
					m.put("id", map.get("fjd"));
					m.put("state", "open");
					List<HashMap> rsList = this.yhService.list("YhMapper.getMenu", m);
					if(rsList.size()<=1){
						this.yhService.update("yhMapper.updateQxState", m);
					}
				}
				this.yhService.insert("YhMapper.deleteQx", map);
			}
			response.getWriter().print("success");
		}catch(Exception e){
			//System.out.println(e);
			response.getWriter().print("error");
		}
		return null;
	}
	
	
	@RequestMapping(value="/kj")
	public String kj(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		Date date = new java.util.Date();
		
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");	
		String kssj = sd.format(date);
		request.setAttribute("kssj", kssj);
		
		int year = date.getYear();
		int month = date.getMonth();
		Calendar cal = Calendar.getInstance();   
		cal.set(Calendar.YEAR, year);   
		cal.set(Calendar.MONTH, month);   
		// 某年某月的第一天   
		int day = cal.getActualMaximum(Calendar.DATE);  
		
		Date date1 = new Date(year,month,day);
		String jssj = sd.format(date1);
		request.setAttribute("jssj", jssj);
		
		List xflxList = this.yhService.list("YhMapper.listXflx", null);
		request.setAttribute("xflxlist", xflxList);
		request.setAttribute("map", map);
		return "/WEB-INF/jsp/yh/kj";
	}
	
	@RequestMapping(value="/xflxjson")
	public String xflxjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		List xflxList = this.yhService.list("YhMapper.listXflx", null);
		JSONArray json = JSONArray.fromObject(xflxList);
		response.getWriter().print(json);
		return null;
	}
	
	@RequestMapping(value="/xflxcz")
	public String xflxcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map=new RequestMap(request);
		String tag = (String)map.get("tag");
		try{
			if("add".equals(tag)){
				this.yhService.insert("YhMapper.insertXflx", map);
			}else if("edit".equals(tag)){
				this.yhService.insert("YhMapper.updateXflx", map);
			}
			else if("delete".equals(tag)){
				this.yhService.insert("YhMapper.deleteXflx", map);
			}
			response.getWriter().print("success");
		}catch(Exception e){
			//System.out.println(e);
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value="/xfxxjson")
	public String xfxxjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		
		HashMap sumMap = this.yhService.getMap("YhMapper.getXfxxHj", map);
		PageBean pageBean = this.yhService.getPage("YhMapper.listXfxx", map);
		
		List<HashMap> rsList = pageBean.getList();
		BigDecimal dxjehj = new BigDecimal(0);
		for(HashMap m:rsList){
			BigDecimal jeB = (BigDecimal)m.get("je");
			dxjehj = dxjehj.add(jeB);
		}
		List rlist = new ArrayList();
		HashMap m = new HashMap();
		m.put("sj", "<font color='red'>本页合计(元)</font>");
		m.put("mc", "<font color='red'>"+dxjehj+"</font>");
		m.put("je", "<font color='red'>合计(元)</font>");
		m.put("hj", "<font color='red'>"+sumMap.get("hj")+"</font>");
		rlist.add(m);
		
		JSONObject jsonO = new JSONObject();
		jsonO.put("rows",rsList);
		jsonO.put("total", pageBean.getRowscount());
		jsonO.put("footer", rlist);
		response.getWriter().print(jsonO);
		return null;
	}
	
	@RequestMapping(value="/xfxxcz")
	public String xfxxcz(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map=new RequestMap(request);
		String tag = (String)map.get("tag");
		try{
			if("add".equals(tag)){
				this.yhService.insert("YhMapper.insertXfxx", map);
			}else if("edit".equals(tag)){
				this.yhService.insert("YhMapper.updateXfxx", map);
			}
			else if("delete".equals(tag)){
				this.yhService.insert("YhMapper.deleteXfxx", map);
			}
			response.getWriter().print("success");
		}catch(Exception e){
			//System.out.println(e);
			response.getWriter().print("error");
		}
		return null;
	}
	
	@RequestMapping(value="/xfhzjson")
	public String xfhzjson(HttpServletRequest request,HttpServletResponse response)throws Exception{
		HashMap map = new RequestMap(request);
		List<HashMap> list = new ArrayList();
		HashMap sumMap = this.yhService.getMap("YhMapper.getMaxXfxx", map);
		if(sumMap != null){
			BigDecimal max = (BigDecimal)sumMap.get("mx");
			//System.out.println("max="+max);
			list = this.yhService.list("YhMapper.listXfhz", map);
			for(HashMap m:list){
				BigDecimal hj = (BigDecimal)m.get("hj");
				//System.out.println("hj===="+hj);
				BigDecimal b = hj.divide(max,2,BigDecimal.ROUND_HALF_UP);
				//System.out.println("b===="+b);
				double d = Math.round(b.multiply(new BigDecimal(100)).doubleValue());
				//System.out.println("d====="+d);
				m.put("bl", d);
			}
		}
		JSONArray json = JSONArray.fromObject(list);
		response.getWriter().print(json);
		return null;
	}
	
}
